//
//  EmoticonTipView.swift
//  HotNews
//
//  Created by YYY on 2017/11/23.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit
import pop

class EmoticonTipView: UIImageView {
  
    //记录表情模型
    private var preEmoticonModel: YYYEmoticonModel?
    
    //表情模型
    var emoticonModel: YYYEmoticonModel?
    {
        didSet
        {
            if preEmoticonModel == emoticonModel
            {
                return
            }
            preEmoticonModel = emoticonModel
            print("设置表情")
            //设置提示按钮的图片和文字
            tipButton.setTitle(emoticonModel?.emoji, for: .normal)
            tipButton.setImage(emoticonModel?.image, for: .normal)
            //设置表情按钮的动画
            let animation: POPSpringAnimation = POPSpringAnimation(propertyNamed: kPOPLayerPositionY)
            animation.fromValue = 30
            animation.toValue = 10
            //[0, 20]. Defaults to 12.
            animation.springSpeed = 20
            //range [0, 20]. Defaults to 4.
            animation.springBounciness = 20
            tipButton.layer.pop_add(animation, forKey: nil)
        }
    }
    
    //提示按钮
    private lazy var tipButton = UIButton()
    
    init()
    {
        let image = UIImage(named: "emoticon_keyboard_magnifier", in: EmoticonManager.shared.bundle, compatibleWith: nil)
        super.init(image: image)
        
        //设置提示视图的锚点
        layer.anchorPoint = CGPoint(x: 0.5, y: 1.2)
        
        tipButton.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        tipButton.frame = CGRect(x: 0, y: 10, width: 36, height: 36)
        tipButton.center.x = center.x
        tipButton.titleLabel?.font = UIFont.systemFont(ofSize: 32)
        //添加提示按钮
        addSubview(tipButton)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
